<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>

    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRole } from '@/api/settings'
import { userPhoto, assignRoles } from '@/api/user'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.loadRole()
  },
  methods: {
    // 给员工分配角色
    async hSubmit() {
      const res = await assignRoles({ id: this.id, roleIds: this.roleIds })
      console.log(res)
      this.$emit('success')
    },
    async loadRole() {
      try {
        const res = await getRole({ page: 1, pagesize: 100 })
        const roleId = await userPhoto(this.id)
        console.log(roleId, 'userPhoto')
        console.log('getRole', res)
        this.list = res.data.rows
        this.roleIds = roleId.data.roleIds
        // 保存数据
      } catch (err) {
        console.log('getRole', err)
      }
    },
    closeDialog() {
      this.$emit('close')
    }
  }
}
</script>
